package com.jiangjun.fetch.scale;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import com.jiangjun.fetch.common.ImageWeibo;

public class Scale4Weibo {

	/**
	 * 保存原图
	 * 
	 * @param url
	 * @param id
	 * @param parentId
	 * @return
	 */
	public static ImageWeibo saveSourcePic(ImageWeibo imageWeibo, int index) {
		String target_path = "";
		String url = imageWeibo.getUrl();
		HttpParams params = new BasicHttpParams();
		params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE,
				false);
		// params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,
		// HTTP.UTF_8);
		params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
				HttpVersion.HTTP_1_1);
		params.setParameter(ClientPNames.COOKIE_POLICY,
				CookiePolicy.BROWSER_COMPATIBILITY);
		params.setParameter(ClientPNames.HANDLE_REDIRECTS, true);
		params.setParameter(ClientPNames.HANDLE_AUTHENTICATION, true);
		params.setParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true);
		params.setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
		params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
		params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);

		HttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		httpGet.setParams(params);
		// 部分网站图片请求返回403
		httpGet.setHeader("User-Agent",
				"Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20100101 Firefox/15.0");
		try {
			HttpResponse response = httpClient.execute(httpGet);
			if (response.getStatusLine().getStatusCode() == 200) {
				ImageImpl imageImpl = new ImageImpl();
				HttpEntity entity = response.getEntity();
				Header contentType = entity.getContentType();
				// 从content-type判断图片的格式
				String type = imageImpl.getImageTypeByContentType(contentType
						.getValue());
				byte[] result = EntityUtils.toByteArray(entity);
				if ("".equals(type)) {
					// 从byte[]中获取图片格式
					type = imageImpl.getImageTypeByByteArray(result);
					if ("".equals(type)) {
						type = imageImpl.getImageType(url);
						if ("".equals(type)) {
							type = "jpg";
						}
					}
				}
				if (type.equalsIgnoreCase("jpeg"))
					type = "jpg";
				target_path = imageImpl.getPath4Weibo(imageWeibo.getId(),
						imageWeibo.getParent_id(), target_path, type, index, 0,
						320);
				imageImpl.saveFile(result, target_path);
				imageWeibo.setType(type);
				imageWeibo.setSource_path(target_path);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return imageWeibo;
	}
}
